﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarCNGDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarCNGDB
    {
        public CarCNGDB()
        {
            //
            // TODO: Add constructor logic here
            
        }

        public Int64 Insert(CarCNG CarCNG)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarCNGInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@ProviderCarCNGId", CarCNG.Providercarcngid);
            _sPara.Add("@ProviderCompanyId", CarCNG.ProviderCompanyId);
            _sPara.Add("@CNG", CarCNG.CNG);
            _sPara.Add("@Rate", CarCNG.Rate);
            _sPara.Add("@RefCEUserId", CarCNG.RefCEUserId);
            _sPara.Add("@Status", CarCNG.Status);
            _sPara.Add("@Finfo1", CarCNG.FInfo1);
            _sPara.Add("@Dinfo1", CarCNG.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CarCNG GetProviderCarCNG(Int64 ProviderCarCNGId)
        {
            CarCNG _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarCNGelectSingleItem]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCarCNGId", ProviderCarCNGId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarCNG();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        public CarCNG GetProviderCarCNG(Int64 ProviderCompanyId, bool CarCNG)
        {
            CarCNG _vehicleyear = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "[sprocProviderCarCNGSelectSingleItemSelected]";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CNG", CarCNG);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _vehicleyear = new CarCNG();
                    _vehicleyear = FillDataRecord(_dt, 0);
                }
            }
            return _vehicleyear;
        }

        private CarCNG FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarCNG myCarCNG = new CarCNG();
            myCarCNG.Providercarcngid = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCarCNGId"].ToString());
            myCarCNG.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarCNG.CNG = Boolean.Parse(myDataTable.Rows[RowId]["CNG"].ToString());
            myCarCNG.Rate = Convert.ToSingle(myDataTable.Rows[RowId]["Rate"].ToString());
            myCarCNG.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarCNG.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myCarCNG.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myCarCNG.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myCarCNG.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myCarCNG.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            return myCarCNG;
        }

    
    }
}
